//record whole object,object must implements Serializable

import java.io.*;
import java.util.*;

public class Main{
	public static void main(String[] args){
		try{
			switch(args[0]){
				case "-s":
					int size=Integer.parseInt(args[1]);
					seperate(args[2],size);
					break;
				case "-c":
					concatenate(args[1],"_");
					break;
				default:
					break;
			}


		}
		catch(ArrayIndexOutOfBoundsException e){
			e.printStackTrace();
		}
		catch(FileNotFoundException e){
			e.printStackTrace();
		}
		catch(IOException e){
			e.printStackTrace();
		}

	}

	public static void seperate(String filename,int size)throws IOException{
		FileInputStream fileinputstream=new FileInputStream(new File(filename));
		BufferedInputStream bufferedinputstream=new BufferedInputStream(fileinputstream);

		byte[] data=new byte[1];
		int count=0;

		count=fileinputstream.available()/size;
		if(fileinputstream.available()%size!=0){
			count++;
		}

		for(int i=0;i<count;i++){
			int num=0;
			File file=new File(filename+"_"+(i+1));
			BufferedOutputStream bufferedoutputstream=new BufferedOutputStream(new FileOutputStream(file));
			while(bufferedinputstream.read(data)!=-1){
				bufferedoutputstream.write(data);
				num++;
				if(num==size){
					bufferedoutputstream.flush();
					bufferedoutputstream.close();
					break;
				}
			}
			if(num<size){
				bufferedoutputstream.flush();
				bufferedoutputstream.close();
			}
		}
		bufferedinputstream.close();
		System.out.println("seperate to "+count+" files");
	}



	public static void concatenate(String filename,String seperatechar)throws IOException{
		List<InputStream> list=new ArrayList<InputStream>();
		int filecount=0;
		int number=0;

		Boolean orgFileExist=false;
		String orgfilename=filename;
		File orgfile=new File(filename);
		orgFileExist=orgfile.exists();

		File file=new File(filename);

		File newfile=new File(getFileName(orgfile.getAbsolutePath())+seperatechar+(filecount+1)+getExt(orgfile.getAbsolutePath()));
//System.out.println("newfile="+newfile);
		do{
			if(newfile.exists()){
				System.out.println(newfile);
//				System.out.println(newfile.getName());
//				System.out.println(getFileName(newfile.getAbsolutePath()));
//				System.out.println(getExt(newfile.getAbsolutePath()));
			
				list.add(filecount,new FileInputStream(newfile));
				filecount++;
				newfile=new File(getFileName(orgfile.getAbsolutePath())+seperatechar+(filecount+1)+getExt(orgfile.getAbsolutePath()));
//System.out.println("next:"+file);
			}
		}
		while(newfile.exists());

		number=filecount;
//method 1--------------------------------------------
/*
		final Iterator<InputStream> iterator=list.iterator();


		Enumeration<InputStream>enumeration=
			new Enumeration<InputStream>(){
				public boolean hasMoreElements(){
					return iterator.hasNext();
				}
				public InputStream nextElement(){
					return iterator.next();
				}
			};
		BufferedInputStream bufferedinputstream=new BufferedInputStream(new SequenceInputStream(enumeration),8192);


		File file=new File(filename);

		if(orgFileExist)
			BufferedOutputStream bufferedoutputstream=new BufferedOutputStream(new FileOutputStream(filename+'2'),8192);
		else
			BufferedOutputStream bufferedoutputstream=new BufferedOutputStream(new FileOutputStream(filename),8192);

		byte[] data=new byte[1];
		while(bufferedinputstream.read(data)!=-1){
			bufferedoutputstream.write(data);
		}
		bufferedinputstream.close();
*/
//method 2--------------------------------------------
/*
//not save in the same name
		filecount=0;
		while(orgFileExist){
			filename=getFileName(orgfilename)+"_C"+filecount+getExt(orgfilename);
			orgfile=new File(filename);
			orgFileExist=orgfile.exists();
			filecount++;
		}
*/

		BufferedOutputStream bufferedoutputstream=new BufferedOutputStream(new FileOutputStream(filename),8192);


		byte[] data=new byte[1];

		for(int i=0;i<list.size();i++){
			BufferedInputStream bufferedinputstream=new BufferedInputStream(list.get(i),8192);

			while(bufferedinputstream.read(data)!=-1){
				bufferedoutputstream.write(data);
			}
			bufferedinputstream.close();
		}

//--------------------------------------------
		bufferedoutputstream.flush();
		bufferedoutputstream.close();
		System.out.println("conbine "+number+" files");
	}


	public static String getFileName(String filename){
		if(filename.indexOf(".")!=-1){
			return filename.substring(0,filename.indexOf("."));
		}
		return filename;
	}

	public static String getExt(String filename){
		if(filename.indexOf(".")!=-1){
			return filename.substring(filename.indexOf("."),filename.length());
		}
		return filename;
	}
}